// This script removes demo app files
import rimraf from 'rimraf';
import fs from 'fs';
import { chalkSuccess } from './chalkConfig';

/* eslint-disable no-console */

const pathsToRemove = [
  './src/components/*',
  './src/images',
  './src/store/*.js',
  './src/store/models/*',
  './src/routes.jsx',
  './src/index.jsx'
];

const filesToCreate = [{
    path: './src/components/emptyTest.spec.js',
    content: '// Must have at least one test file in this directory or Mocha will throw an error.'
  },
  {
    path: './src/index.jsx',
    content: `
/* eslint-disable import/default */

require('./favicon.ico');
import './styles/styles.less';

import React from 'react';
import ReactDOM from 'react-dom';

ReactDOM.render(
  <h1> Hello, world! </h1>,
  document.getElementById('app')
);
    `
  }
];

function removePath(path, callback) {
  rimraf(path, error => {
    if (error) throw new Error(error);
    callback();
  });
}

function createFile(file) {
  fs.writeFile(file.path, file.content, error => {
    if (error) throw new Error(error);
  });
}

let numPathsRemoved = 0;
pathsToRemove.map(path => {
  removePath(path, () => {
    numPathsRemoved++;
    if (numPathsRemoved === pathsToRemove.length) { // All paths have been processed
      // Now we can create files since we're done deleting.
      filesToCreate.map(file => createFile(file));
    }
  });
});

console.log(chalkSuccess('Demo app removed.'));
